package sword.chapter2Array;

/**
 * 两数之和
 * 双指针反向：排序数组中两数之和
 * 双指针同向：正整数中子数组的和或乘积
 *
 * @author K
 * @date 2021-10-31 20:55
 */
public class S6TwoNumberSum {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 4, 6, 10};
        int[] result = twoNumberSum(nums, 8);
        System.out.println();
    }

    public static int[] twoNumberSum(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start < end && nums[start] + nums[end] != target) {
            if (nums[start] + nums[end] > target) {
                end--;
            } else {
                start++;
            }
        }
        return new int[]{start, end};
    }
}
